<?php
/**
 * Created by Bruce.
 * IDE: PhpStorm
 * Date: 2018/8/7
 * Time: 17:53
 */

namespace app\park\model;


use think\Db;

class AdminQueryBillModel
{
	private $db;

	function __construct()
	{
		$this->db = Db::connect();
	}

	public function queryByDate($openId, $date)
	{
		$sql = "SELECT count(p.fee) as nu,CAST(SUM(p.fee) AS DECIMAL (19, 2))as fee,DATE_FORMAT(pay_time,'%m') as months,park_info.space,park_info.occupied_space FROM park_info join pay_info as p on park_info.open_id=? AND DATE_FORMAT(p.pay_time, '%Y%m') = DATE_FORMAT(?,'%Y%m' ) group by months;";
		$res = $this->db->query($sql, [$openId, $date]);
		if (empty($res)) {
			return ['code' => '200', 'result' => 'null'];
		} else {
			$res = $res[0];
			$response = ['code' => '200', 'result' => $res];
			return $response;
		}

	}

	public function queryBill($openId, $start, $nu)
	{
		$sql = "SELECT b.bill_id, b.order_time, b.space_id, b.park_id,b.state,user_info.car_id, pay1.fee AS order_fee,pay2.fee AS park_fee FROM bill AS b JOIN park_info ON park_info.park_id = b.park_id AND park_info.open_id =? JOIN user_info ON user_info.open_id = b.open_id LEFT JOIN pay_info AS pay1 ON b.order_pay_id = pay1.pay_id LEFT JOIN pay_info AS pay2 ON b.park_pay_id = pay2.pay_id ORDER BY b.order_time ASC LIMIT ?,?";

		$res = $this->db->query($sql, [$openId, $start, $nu]);
		$response = ['code' => '200'];
		if (!empty($res)) {
			foreach ($res as $v) {

			}
			$response['result'] = $res;
		} else {
			$response['result'] = 'null';
		}
		return $response;
	}

	function __destruct()
	{
		// TODO: Implement __destruct() method.
		if ($this->db != null) {
			$this->db->close();
		}
	}
}
